package gameObject;

import ui.ImageUtil;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

/*
控制抓手的类
 */
public class Grasp {
    BufferedImage hand = ImageUtil.getImage("/Images/抓手.png");
    //抓手的位置
    int x = 425;
    int y = 92;
    int width = hand.getWidth();
    int height = hand.getHeight();
    double degree = 0;
    int speed = 1; //抓手旋转的速度
    int dir;
    boolean pause;
    double fac = Math.sqrt(x*x + y*y)-100;
    Wife wife;
    public Grasp(Wife wife){
        this.wife = wife;
    }
    public void Drawing(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        g.setColor(Color.BLACK);
        //设置画笔的粗细
        Stroke stroke = new BasicStroke(5.0f);
        g2.setStroke(stroke);
        g2.drawLine(430,92,x+20,y+5);
        g2.rotate(Math.toRadians(degree),x+5,y);
        g2.drawImage(hand,x,y,null);
        if (pause == true){
            //System.out.println(degree);
            //使抓手按照一定弧线运动
            x = (int) (x - (fac * (Math.sin(Math.toRadians(degree))))/20);
            y = (int) (y + (fac * (Math.cos(Math.toRadians(degree))))/20);
            if (x < width || y > 800-height || x > 1000-width){
                this.x = 425;
                this.y = 92;
                pause = false;
            }else if (this.getRect().intersects(wife.getRect())){
                pause = false;
            }
        }
    }
    //抓手的运动方式，360度旋转
    public void move(){
//        x = 450 + 100*Math.cos(degree);
//        y = 150 + 100*Math.sin(degree);
//        degree += 0.1;
        degree += speed;
    }
    //45度到-45度
    public void move2(){
        //如果pause为false，则抓手运动
        if (pause == false){
            if (y <= 92){
                this.x = 425;
                if (dir == 0){
                    degree += speed;
                    if (degree > 45){
                        dir = 1;
                    }
                }else if (dir == 1){
                    degree -= speed;
                    if (degree <-45){
                        dir = 0;
                    }
                }
            }else{
                x = (int) (x + (fac * (Math.sin(Math.toRadians(degree))))/50);
                y = (int) (y - (fac * (Math.cos(Math.toRadians(degree))))/50);
            }
        }
    }
    //获取抓手的角度
    public double getDegree() {
        return degree;
    }
    //抓手方向的控制
    public void setDir(int dir) {
        this.dir = dir;
    }
    //返回抓手的矩形，用于碰撞检测
    public Rectangle getRect(){
        return new Rectangle(this.x,this.y,this.width,this.height);
    }
    public void addKeyListener(KeyEvent e){
        if (e.getKeyCode() == KeyEvent.VK_ENTER){
            //如果按下Enter,则暂停
            pause = true;
        }
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}
